package IT.DataAccess;

import IT.Entities.Almacen;
import java.util.List;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatAlmacen implements IDatAlmacen{
    private Almacen oAlmacen;
    private List<Almacen> oLista;
    private BDConexion oBase;
    
    private Connection oConexion;
    private CallableStatement oCst;
    private ResultSet oRst;
    
    public List<Almacen> Listar() {
        oLista = new ArrayList<Almacen>();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenListar()}");          
            oRst = oCst.executeQuery();

            while (oRst.next()) {             
                oLista.add(new Almacen(oRst.getInt("cod_alm_in"), 
                        oRst.getString("nom_alm_vc"),
                        oRst.getInt("est_alm_in"),
                        oRst.getString("Estado")
                        ));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }

        return oLista;
    }

    public Almacen Ind(int nCodigo) {
       oAlmacen=new Almacen();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {             
                oAlmacen=new Almacen(oRst.getInt("cod_alm_in"), 
                        oRst.getString("nom_alm_vc"),
                        oRst.getInt("est_alm_in"),
                        oRst.getString("Estado"));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }

        return oAlmacen;
    }

    public void Insertar(Almacen ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenInsertar(?,?)}");              
            oCst.setString(1, ent.getNom_alm_vc());
            oCst.setInt(2, ent.getEst_alm_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(Almacen ent) {
       try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenEditar(?,?,?)}");
            
            oCst.setInt(1, ent.getCod_alm_in());
            oCst.setString(2, ent.getNom_alm_vc());
            oCst.setInt(3, ent.getEst_alm_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Eliminar(int nCodigo) {
        int nResultado=0;
        
          try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenEliminar(?,?)}");
            oCst.setInt(1, nCodigo);
            oCst.registerOutParameter(2, java.sql.Types.INTEGER);
            oCst.execute();
            nResultado=oCst.getInt(2);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return nResultado;
    }

    public int Valida(int nCodigo, String cAlmacen) {
         int nValida=0;        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AlmacenValida(?,?,?)}");              
            oCst.setInt(1, nCodigo);
            oCst.setString(2, cAlmacen);
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            oCst.execute();
            nValida=oCst.getInt(3);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nValida;
    }

}
